#include <DHT.h>
#include <Xively.h>
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#define DHTPIN 9     // Indicamos el pin donde conectaremos la patilla data de nuestro sensor
#define DHTTYPE DHT11   // DHT 11 


// MAC address for your Ethernet shield
byte mac[] = { 0x4C, 0x72, 0xB9, 0x98, 0xDD, 0x1B };

// Your Xively key to let you upload data
char xivelyKey[] = "hT2ww3c2PBKxdvQKXYqB2h-nwdiSAKx0dzRYOU5sTEZwRT0g";//"0EvyVoy3p9kQIBJ5-7LPFbWoDxGSAKx3dTMvdUh3blRYRT0g";

DHT dht(DHTPIN, DHTTYPE);  //Indica el pin con el que trabajamos y el tipo de sensor
int maxh=0, minh=100, maxt=0, mint=100;
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorTemperaturePin = 0;
int sensorLuminosityPin = 1;
//int sensorHumidityPin = 9;
int input;


// Define the strings for our datastream IDs
char sensorTemperatura[] = "Temperature";
char bufferId[] = "Check_Error";
char sensorHumedad[] = "Humedad";
char sensorLuminosidad[] = "Luminosity";

String stringId("random_string");
const int bufferSize = 140;
char bufferValue[bufferSize]; // enough space to store the string we're going to send
XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorTemperatura, strlen(sensorTemperatura), DATASTREAM_FLOAT),
  XivelyDatastream(sensorHumedad, strlen(sensorHumedad), DATASTREAM_FLOAT),
  XivelyDatastream(sensorLuminosidad, strlen(sensorLuminosidad), DATASTREAM_FLOAT),
  XivelyDatastream(bufferId, strlen(bufferId), DATASTREAM_BUFFER, bufferValue, bufferSize),
  };
// Finally, wrap the datastreams into a feed
XivelyFeed feed(1631431830, datastreams, 4 /* number of datastreams */);

EthernetClient client;
XivelyClient  xivelyclient(client);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println("Starting multiple datastream upload to Cosm...");
  Serial.println();
  Serial.println("Comprobacion sensor DHTxx:");
  //Iniciamos el sensor
  dht.begin();
  pinMode(13, OUTPUT);

  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(15000);
  }
  //Mensaje de inicio
  
}

void loop() {
  if (Serial.available() > 0){
    input=Serial.read();
    
    Serial.println("Leyendo");
    
      if ( input =='1')
        {
          if(digitalRead(2) == LOW){
        Serial.println("Recibido 1 ");
        digitalWrite(2, HIGH); //Si el valor de input es 1, se enciende el led
          }
        }
      
     
     else if (input =='2' )
        {
          if(digitalRead(6) == LOW){
          digitalWrite(6,HIGH); // Si el valor es dos se enciende el aire acondicionado
          }
        }
        
        
      else if (input =='3')
        {
          if(digitalRead(2) == HIGH){
        digitalWrite(2, LOW); //Si el valor de input es 1, se enciende el led
          }
        }
        
        
       else if (input =='4')
        {
          if(digitalRead(6)== HIGH){
          digitalWrite(6,LOW); // Si el valor es dos se enciende el aire acondicionado
          }
        }
        
    
  
  
  
}

int sensorTemperatureValue = (((analogRead(sensorTemperaturePin)/ 1023.0) * 5.0) * 100.0);
  sensorTemperatureValue = sensorTemperatureValue - 303.15;
  datastreams[0].setFloat(sensorTemperatureValue);

  Serial.print("Read sensor value ");
  Serial.println(datastreams[0].getFloat());

  int sensorLuminosityValue = analogRead(sensorLuminosityPin);
  datastreams[2].setFloat(sensorLuminosityValue);

  Serial.print("Read sensor value ");
  Serial.println(datastreams[2].getFloat());

  int h = dht.readHumidity();  //Guarda la lectura de la humedad en la variable float h

  int sensorHumidityValue = analogRead(DHTPIN);
   datastreams[1].setFloat(h);
   
   Serial.print("Read sensor value ");
   Serial.println(datastreams[1].getFloat());



  datastreams[3].setBuffer("ok");
  Serial.print("Setting buffer value to:\n    ");
  Serial.println(datastreams[3].getBuffer());

  // Pick a random number to send up in a string
  /* String stringValue(random(100));
   stringValue += " is a random number";
   datastreams[2].setString(stringValue);
   Serial.print("Setting string value to:\n    ");
   Serial.println(datastreams[2].getString());*/

  Serial.println("Uploading it to Xively");
  int ret = xivelyclient.put(feed, xivelyKey);
  Serial.print("XivelyClient.put returned ");
  Serial.println(ret);

  Serial.println();
  delay(2500);

}
